/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.yonsei.iwb.sdt.guix;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;

import java.awt.List;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Vector;
import javax.swing.BorderFactory;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListModel;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author DK
 */
public class TargetInstanceViewFrame extends JPanel implements ActionListener, ItemListener {

    private GDLearningPanel mf;
    private Controller ctrl;
    private JTable targetInsTable;
    private DefaultTableModel targetInsTableModel;
    private JScrollPane tiScrollPane;  
    private JList list;
    private JButton setins;
    
    
    private JFrame jf;
    private boolean listFilled = false;
//    private Model model;
    DefaultListModel listmodel;
    int counter = 15;
    
    DualList dual;
            
    public TargetInstanceViewFrame(GDLearningPanel mf) {
        this.mf = mf;
        this.ctrl = this.mf.getController();
        this.setins = new JButton("Set Target Instances");
        this.setins.addActionListener(this);
        dual = new DualList();
        this.add(dual);
        this.add(setins);
        this.jf = new JFrame("Positive and Negative Instance");
//        this.jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        
//        this.jf.getContentPane().add(dual, BorderLayout.CENTER);
        this.jf.setSize(700, 500);
        this.jf.add(this);

        this.setVisible(true);
        this.setBorder(BorderFactory.createTitledBorder(null, "Target Instance", TitledBorder.CENTER, TitledBorder.DEFAULT_POSITION, new Font("맑은 고딕", 1, 12)));
        this.setBackground(Color.WHITE);



    }
    
    public void clearTargetInsList() {
        
        this.dual.clearDestinationListModel();
        this.dual.clearSourceListModel();
//        while (this.list.getItemCount() > 0) {
//            this.list.removeAll();
//        }

    }

//    public void clearTargetInsTable() {
//        while (this.targetInsTableModel.getRowCount() > 0) {
//            this.targetInsTableModel.removeRow(0);
//        }
//
//    }
    public void addTargetInstanceValue(Vector<Resource> rset) {
        for (int i = 0; i < rset.size(); i++) {
//            JCheckBox cb = new JCheckBox();
            String value[] = {rset.get(i).toString()};
            dual.addSourceElements(value);
            
//            System.out.println(rset.get(i).toString());
            
        }
        this.listFilled = true;
        this.showFrame();
        
        
    }
    
    public void addTargetInstanceValue(java.util.List<String> rset) {
        for (int i = 0; i < rset.size(); i++) {
//            JCheckBox cb = new JCheckBox();
            String value[] = {rset.get(i)};
            dual.addSourceElements(value);
            
//            System.out.println(rset.get(i).toString());
            
        }
        this.listFilled = true;
        this.showFrame();
        
        
    }
    
    public void showFrame(){
        this.jf.setPreferredSize(getMaximumSize());
        this.jf.setVisible(true);
    }
    
    public void closeFrame(){
        this.jf.dispose();
    }
    
    public boolean listFilled(){
        return this.listFilled;
    }
//    public void addTargetInstanceValue(Vector<Resource> rset) {
//        for (int i = 0; i < rset.size(); i++) {
//            JCheckBox cb = new JCheckBox();
//            Object value[] = {cb, rset.get(i).toString()};
//            this.targetInsTableModel.addRow(value);
//        }
//    }

    @Override
    public void itemStateChanged(ItemEvent e) {        
//        String s = this.list.getSelectedItem();
//        this.mf.outputTextArea.append(s+"\n");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object o = e.getSource();
        
        if(o == this.setins){
//            this.dual.showListValue();            
            
//            Vector<String> removeSet = new Vector<String>();
//            int[] itemNum = this.list.getSelectedIndexes();
//            String[] itemStr = this.list.getSelectedItems();
//            if(itemNum.length > 0){
//                for (int i = 0; i < itemNum.length; i++) {
//                    String s = itemStr[i];
//                    this.posSet.add(s);
//                    this.list.remove(s);
//                }
//            }
//            String[] negset = this.list.getItems();
//            for (int i = 0; i < negset.length; i++) {
////                String string = negset[i];
//                this.negSet.add(negset[i]);
//                
//            }
//            
            Vector<String> posSet = this.dual.getPositiveInstanceValueSet();
            Vector<String> negSet = this.dual.getNegativeInstanceValueSet();
            
//            for (int i = 0; i < posSet.size(); i++) {
//                System.out.println("p : " + posSet.get(i));
//            }
//            for (int i = 0; i < negSet.size(); i++) {
//                System.out.println("n : " + negSet.get(i));                
//            }
            
            this.ctrl.setPOSNEG_Instances(posSet, negSet);
            
        }
        
        this.closeFrame();
        
    }
}
